package com.gouwudai.gobbs.dao.impl;

import java.util.List;
import java.util.Map;

import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IClassUserDAO;
import com.gouwudai.gobbs.pojo.TbbsClassUser;
import com.litt.core.common.PageParam;
import com.litt.core.common.Utility;
import com.litt.core.exception.BusiException;

/**
 * 
 *  <b>Title:</b> 版面成员表(认证用户\版主)
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @date 2007-6-19
 * @version 1.0
 * <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容: 
 * </pre>
 */
public class ClassUserDAOImpl extends BaseDAOImpl implements IClassUserDAO
{
    /**
     * 保存版面成员信息
     * @param tbbsClassUser
     * @throws BusiException
     */
    public void save(TbbsClassUser tbbsClassUser)
    {
        super.saveObject(tbbsClassUser);
    }

    /**
     * 删除版面成员信息
     * @param cuId
     */
    public void delete(Integer cuId)
    {
        super.deleteObject(TbbsClassUser.class.getName(),"cuId",cuId);
    }
    
    /**
     * 批量删除版面成员信息
     * @param cuIds
     */
    public void deleteBatch(String cuIds)
    {
        super.deleteObjects(TbbsClassUser.class.getName(),"cuId",cuIds);
    }    
    
   /**
     * 查询分类/版面版主
     * @param classId
     * @return List
     */
    public List listClassManager(Integer classId)
    {  
        String hql = "select b from TbbsClassUser a, TbbsUserInfo b where a.classId="+classId+" and a.userType=2 and a.userId=b.userId";
        return super.listAll(hql);
    } 
    
    /**
     * 分页查询
     * @param pageParam 分页查询对象
     * @return IPageList
     */
    public IPageList listClassUserByPage(PageParam pageParam)
    {       
        Map paramMap = pageParam.getConds();
        String loginId = (String)paramMap.get("loginId");
        Integer classId = (Integer)paramMap.get("classId");
        Integer userType = (Integer)paramMap.get("userType");
        
        StringBuilder listSql = new StringBuilder("SELECT A.CU_ID, A.CLASS_ID, B.CLASS_NAME,B.CLASS_TYPE,B.CLASS_PRO, C.* FROM TbbsClassUser A ,TbbsClass B,TbbsUserInfo C WHERE A.USER_TYPE="+userType+" AND (A.USER_ID IS NOT NULL AND A.USER_ID=C.USER_ID) AND A.CLASS_ID=B.CLASS_ID");
        if(classId.intValue()>0)
        {
            listSql.append(" AND A.CLASS_ID=");  
            listSql.append(classId);
        }
        if(!"".equals(loginId))
        {
            listSql.append(" AND C.LOGIN_ID='");  
            listSql.append(loginId);
            listSql.append("' ");
        }
        if(!Utility.isEmpty(pageParam.getSort()))
            listSql.append(pageParam.getSort());
        return super.listSqlPage(listSql.toString(),pageParam.getPageIndex(),pageParam.getPageSize());
    }     
    
    /**
     * 查询分类/版面认证用户
     * @param classId
     * @return List
     */
    public List listClassAuthUser(Integer classId)
    {  
        String hql = "select userId from TbbsClassUser where classId="+classId+" and userType=1";
        return super.listAll(hql);
    }
    
    /**
     * 查询指定用户被认证过的版面
     * @param userId
     * @return List
     */
    public List listAuthUserClass(Integer userId)
    {  
        String hql = "select classId from TbbsClassUser where userId="+userId+" and userType=1";
        return super.listAll(hql);
    } 
    
    /**
     * 查询指定用户是否被指定版面认证过.
     * 
     * @param classId 
     * @param userId 
     * 
     * @return boolean
     */
    public boolean hasClassAuth(Integer classId,Integer userId)
    {  
        String listCountHql = "select count(*) from TbbsClassUser where classId="+classId+" and userId="+userId+" and userType=1";
        int count = super.listHqlCount(listCountHql);
        if(count>0)
           return true;
        else
           return false;
    }     
    
    /**
     * 查询当前用户是否为分类/版面版主
     * @param classId
     * @param userId 
     * @return boolean
     */
    public boolean isClassManager(Integer classId,Integer userId)
    {  
        String listCountHql = "select count(*) from TbbsClassUser a, TbbsUserInfo b where a.classId="+classId+" and a.userId="+userId+" and a.userType=2 and a.userId=b.userId";
        int count = super.listHqlCount(listCountHql);
        if(count>0)
           return true;
        else
           return false;
    }  
    
    
    /**
     * 查询所有没有设置过版面用户的有效用户.
     * 
     * @param userType 版面用户类型: 1-认证用户,2-版主
     * @param classId 版面ID
     * 
     * @return the list
     */
    public List listAllUserClassNotHave(Integer classId,Integer userType)
    { 
        String hql = "from TbbsUserInfo a where a.isdelFlag=0 and a.userId not in (select b.userId from TbbsClassUser b where b.classId="+classId+" and userType="+userType+")";
        return super.listAll(hql);
    } 
    
}
